package com.nebula.service.impl;

import com.nebula.entity.Navigation;
import com.nebula.common.Level2Result;
import com.nebula.common.TreeResult;
import com.nebula.dao.IMenuDao;
import com.nebula.dao.impl.MenuDaoImpl;
import com.nebula.service.IMenuService;
import com.nebula.util.TreeNodeUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CcccX
 * @create 2019-01-11 15:45
 */
public class MenuServiceImpl implements IMenuService {
    private IMenuDao iMenuDao = new MenuDaoImpl();

    @Override
    public ArrayList<TreeResult> menuTree(int roleId) {
        //查询所有导航栏
        List<Navigation> navigations = iMenuDao.findAll();
        //通过roleId查询该角色权限所对应的导航栏
        List<Navigation> list = iMenuDao.selectByRoleId(roleId);
        ArrayList<TreeResult> tree = new ArrayList<>();
        String pname = "";
        for (Navigation navigation : navigations) {
            TreeResult treeResult = new TreeResult();
            if ("0".equals(navigation.getParentId())) {
                treeResult.setState("open");
            } else {
                treeResult.setState(null);
            }
            for (Navigation navigation1 : list) {
                if (!"0".equals(navigation.getParentId())) {
                    if (navigation.getId().equals(navigation1.getId())) {
                        treeResult.setChecked(true);
                        break;
                    } else {
                        treeResult.setChecked(false);
                    }
                }
            }
            treeResult.setId(navigation.getId());
            treeResult.setText(navigation.getItem());
            treeResult.setIconCls(navigation.getIcon());
            treeResult.setUrl(navigation.getUrl());
            treeResult.setParentId(navigation.getParentId());
            treeResult.setDesc(navigation.getInstruction());
            treeResult.setSort(navigation.getSort());
            tree.add(treeResult);
        }
        return TreeNodeUtil.getTree(tree);
    }

    @Override
    public List<TreeResult> selectParentNode(int roleId) throws SQLException {
        List<Navigation> list = iMenuDao.selectParentNode(roleId);
        List<TreeResult> treeResults = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            TreeResult treeResult = assembleTreeResult(list.get(i));
            if (i == 0) {
                treeResult.setState("open");
            } else {
                treeResult.setState("closed");
            }
            treeResults.add(treeResult);
        }
        return treeResults;
    }

    @Override
    public List<Level2Result> selectLevel2(int roleId, String parentId) throws SQLException {
        List<Navigation> list = iMenuDao.selectLevel2(roleId, parentId);
        List<Level2Result> level2Results = new ArrayList<>();
        for (Navigation aList : list) {
            Level2Result level2Result = assembleLevel2Result(aList);
            level2Results.add(level2Result);
        }
        return level2Results;
    }

    private TreeResult assembleTreeResult(Navigation navigation) {
        TreeResult treeResult = new TreeResult();
        treeResult.setId(navigation.getId());
        treeResult.setText(navigation.getItem());
        treeResult.setIconCls(navigation.getIcon());
        treeResult.setUrl(navigation.getUrl());
        treeResult.setParentId(navigation.getParentId());
        treeResult.setState("closed");
        //treeResult.setChecked(false);
        return treeResult;
    }

    private Level2Result assembleLevel2Result(Navigation navigation){
        Level2Result level2Result = new Level2Result();
        level2Result.setId(navigation.getId());
        level2Result.setIconCls(navigation.getIcon());
        if (navigation.getUrl().indexOf("?") != -1) {
        	level2Result.setAttributes(navigation.getUrl() + "&id=" + navigation.getId());
		}else {
			level2Result.setAttributes(navigation.getUrl() + "?id=" + navigation.getId());
		}

        level2Result.setText(navigation.getItem());
        return level2Result;
    }
}
